package com.database.findmystuff;
import android.content.Context;
import android.database.*;
import android.database.sqlite.*;
import android.database.sqlite.SQLiteDatabase.CursorFactory;


public class DBHelper extends SQLiteOpenHelper{
	/**
	 * the constructor for the DBHelper Method
	 * @param context
	 * @param name
	 * @param factory
	 * @param version
	 */
	public DBHelper(Context context, String name, CursorFactory factory, int version) {
		super(context, name, factory, version);
		// TODO Auto-generated constructor stub
		//not sure what to do here yet
	}

	//The Android's default system path of your application database.
	private static String DB_PATH = "/data/data/com.example.findmystuff/databases/";
	private String dbName;
	private SQLiteDatabase db; 
	/**
	 * This method creates a database using sql commands
	 * @param dbName the name of the database
	 */
	public void createDB(String dbName){
		this.dbName=dbName;
		db.execSQL("CREATE TABLE  `"+dbName+"`.`Users` (`Email` VARCHAR( 100 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,`Password` VARCHAR( 100 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,PRIMARY KEY (  `Email` ) ,UNIQUE (`Password`)");
	}
	/**
	 * This method creates a new account
	 * @param email the email to be inserted into the database
	 * @param password the password to be inserted into the database
	 */
	public void newAccount(String email, String password){
		   /* Insert data to a Table*/
		db.execSQL("INSERT INTO  `findmystuff`.`Users` (`Email` ,`Password`)VALUES (CHAR(  '"+ email +"' ) , CHAR(  '"+password+"' )");
	}
	/**
	 * used to check the user login against an sql database
	 * @param email the user's email
	 * @param password the user's password
	 * @return returns the account of the user
	 */
	public String[] CheckUserLogin(String email, String password){
		Cursor cursor = db.query(true, dbName, null, "Email" + "=" + email, null, null, null, null, null);


	    if ((cursor.getCount() == 0) || !cursor.moveToFirst()) {
	        return null;
	    }

	    String[] account = new String[2]; 
	    account[0] = cursor.getString(cursor.getColumnIndex("Email"));
	    account[1] = cursor.getString(cursor.getColumnIndex("Password"));
	    return account;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		//not sure what to do with these yet
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		//not sure what to do with these yet
	}
}